<?php
	session_start();
	if (isset($_SESSION['user_id']))
	{
        $ret = 0;
		include("config.php");
            include("convert_bugID.php");
		$user_id = $_SESSION['user_id'];
		
		$bug_id = $_GET["bug_id"];
		$bug_title = $_POST["txtBugTitle"];
		$bug_description = $_POST["txtBugDescription"];
		$bug_severity_id =  $_POST["severity"];
		$bug_category_id =  $_POST["category"];
		$bug_techrepro_id =  $_POST["techrepro"];
        $bug_device_id = $_POST["device"];
        $bug_status_id =  $_POST["status"];
		$bug_project_id = $_POST["project"];
        $date = date("Y-m-d H:i:s");
        //$date.=" 7:0:0";
        $bug_old_status = "";

		$sql_load_bug = "select * from bug where bug_id = $bug_id";
		if (mysql_query($sql_load_bug))
		{
			$bug_info = mysql_fetch_row(mysql_query($sql_load_bug));
            /*
			echo "<br>".$bug_info["0"];//bug_id
			echo "<br>".$bug_info["1"];//title
			echo "<br>".$bug_info["2"];//description
			echo "<br>".$bug_info["3"];//severity_id
			echo "<br>".$bug_info["4"];//category_id
			echo "<br>".$bug_info["5"];//techrepro_id
			echo "<br>".$bug_info["6"];//device_id
			echo "<br>".$bug_info["7"];//status_id
            echo "<br>".$bug_info["8"];//project_id
			echo "<br>".date("Y-m-d");
            */
            $bug_old_status = $bug_info["7"];
		}
		else
		{
			$ret = 1;
		}
        if ($bug_status_id != $bug_old_status)
        {
            $sql_insert_status_bug = "";
            if ($bug_status_id == 2)//fixed
            {
                 $sql_insert_status_bug = "insert into bug_fixer (bug_fixer_id,bug_id,fixer_id,status_id,date_post) values
                                                                    (null,$bug_id,$user_id,$bug_status_id,UNIX_TIMESTAMP ('$date'))
                                            ";

            }
            else
            {
                 $sql_insert_status_bug = "insert into bug_tester (bug_tester_id,bug_id,tester_id,status_id,date_post) values
                                                                    (null,$bug_id,$user_id,$bug_status_id,UNIX_TIMESTAMP ('$date'))
                                            ";
            }
            if(mysql_query($sql_insert_status_bug))
            {
              
            }
            else
            {
                $ret = 1;
            }
           
        }

        $sql_update = "update bug set 
                                    title='".convert_special_string($bug_title)."',
                                    description='".convert_special_string($bug_description)."',
                                    severity_id=$bug_severity_id,
                                    category_id=$bug_category_id,
                                    techrepro_id=$bug_techrepro_id,
                                    device_id=$bug_device_id,
                                    status_id= $bug_status_id,
                                    project_id=$bug_project_id
                         where bug_id = $bug_id";
         //echo "<br>".$sql_update;
         if(mysql_query($sql_update))
         {
            
         }
         else
         {
                $ret = 1;
         }
         if ($ret == 0)
         {
              //include("../VIEW/frmUpdateBug.php");
              echo "<script>window.location.href='../VIEW/frmMain.php'</script>";
         }
         else
         {
              echo "<script>alert('update unsuccessfully');window.location.href='../VIEW/frmMain.php'</script>";
         }
	}
	else
	{
		
        echo "<script>window.location.href='../index.php'</script>";
	}


?>